/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import DAO.AlbumDAO;
import DAO.ArtistDAO;
import DAO.GenreDAO;
import DAO.PlaylistDAO;
import DAO.SongDAO;
import DAO.UserDAO;
import DTO.album.Albums;
import DTO.artist.Artists;
import DTO.genre.Genres;
import DTO.playlist.Playlists;
import DTO.song.Songs;
import DTO.user.Users;
import java.io.PrintWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author NoiR
 */
public class XMLGenerator {
    // ThuanNH

    // ThuanNH
    public void generateSongXML() {
        String fileName = "Songs.xml";
        SongDAO songDB = new SongDAO();
        Songs songs = songDB.getDatabase();

        if (songs == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(songs, fileName);
    }

    //TanND
    public static void generateArtistsXML() {
        String fileName = "Artists.xml";
        ArtistDAO artistDAO = new ArtistDAO();
        Artists artists = artistDAO.selectAllArtists();
        if (artists == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(artists, fileName);
    }

    //TanND
    public void generateGenresXML() {
        String fileName = "Genres.xml";
        GenreDAO genreDAO = new GenreDAO();
        Genres genres = genreDAO.selectAllGenres();
        if (genres == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(genres, fileName);
    }

    // TungNT
    public void generateUserXML() {
        String fileName = "Users.xml";
        UserDAO userDAO = new UserDAO();
        Users users = userDAO.selectAllUsers();
        if (users == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(users, fileName);
    }

    // ThuanNH
    public void generateXML(Object list, PrintWriter pw) {
        try {
            if (list != null) {
                JAXBContext ctx = JAXBContext.newInstance(list.getClass());
                Marshaller mar = ctx.createMarshaller();
                mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                mar.marshal(list, pw);
            }
            System.out.println("Generate successfully ");
        } catch (JAXBException ex) {
            System.out.println("Generate XML error: " + ex.getMessage());
        } finally {
            if (list == null) {
                System.out.println("Get Database error.");
            }
        }
    }

    //DinhNP
    public void generateAlbumsXML() {

        String fileName = "Albums.xml";

        AlbumDAO albumsDAO = new AlbumDAO();
        Albums albums = new Albums();
        albums = albumsDAO.getAllAlbums();

        if (albums == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(albums, fileName);
    }

    //DinhNP
    public void generatePlaylistsXML() {
        String fileName = "Playlists.xml";
        Playlists playlists = new Playlists();
        PlaylistDAO playlistsDAO = new PlaylistDAO();
        playlists = playlistsDAO.getAllPlaylists();

        if (playlists == null) {
            return;
        }
        // do marshall
        XMLUtils.marshalToXMLFile(playlists, fileName);
    }
}
